package com.minyifang.wcspf.work.ui.user.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.MakeToken;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.TimeStampBean;
import com.minyifang.wcspf.work.ui.user.view.LoginView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xuyougen on 2018/6/5.
 */

public class LoginPresenter extends BasePresenter<LoginView> {

    private String localToken;
    private String iv, key; // 加解密数据使用

    public void doLogin(final String phone, final String pwd) {
        getView().showLoadingView();
        final JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getTimeStamps(requestBody).flatMap((Function<HttpRespond<TimeStampBean>, ObservableSource<HttpRespond>>) timeStampBeanHttpRespond -> {
            LogUtil.e(timeStampBeanHttpRespond.data.toString());

            Map<String, Object> maps = new HashMap<>();
            maps.put("username", phone);
            maps.put("ticksid", timeStampBeanHttpRespond.data.id);
            iv = MakeToken.getIv(pwd, timeStampBeanHttpRespond.data.id);
            key = MakeToken.getKey(phone, timeStampBeanHttpRespond.data.val);

            localToken = MakeToken.getToken(phone, pwd, timeStampBeanHttpRespond.data.id, timeStampBeanHttpRespond.data.val);
            JSONObject data = Constants.getJsonData(maps, localToken);

            RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), data.toString());
            return RetrofitHelper.getInstance().getService().login(requestBody1);
        }), httpRespond -> {
            getView().hideLoadingView();
            getView().loginRes(httpRespond);
        });
    }

    public void WeChatLogin(String uid, String name, String iconurl) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("name", name);
        map.put("iconurl", iconurl);
        JSONObject jsonData = Constants.getJsonData(map, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().WeChatLogin(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showWeChatLoginRes(respond.data);
            } else
                getView().showRequestFailed(respond.message);
        });
    }

    public String getLocalToken() {
        return localToken;
    }

    public String getIv() {
        return iv;
    }

    public String getKey() {
        return key;
    }


}
